#include "stdafx.h"

#include "GroupProductTable.h"

CSGroupTbl::CSGroupTbl()
{
	m_sName = TABLE_GROUP_PRODUCT;
}
 bool CSGroupTbl::addNewGroup(const CGroupProduct* pGroup)
 {
	 CString sQueryAdd;
	 sQueryAdd.Format(_T("( '%s',_utf8'%s',%d,_utf8'%s')")
		 ,pGroup->getCode(),pGroup->getName(),pGroup->getType(),pGroup->getNote());
	 CSqlTable::m_sName = TABLE_GROUP_PRODUCT;
	return insertRecord(TABLE_GROUP_PRODUCT,sQueryAdd);
 }

 bool CSGroupTbl::deleteGroup(const CString& sCode)
 {
	 CString sValue;
	 bool bRes = false;
	 sValue.Format(_T("DELETE FROM %s.%s WHERE %s = '%s'"),g_sDatabase,TABLE_GROUP_PRODUCT,FIELD_CODE_GROUP,sCode);		
	 if (conn)
	 {
		 CT2A ascii(sValue, CP_UTF8);
		 if(mysql_query(conn,ascii.m_psz) == 0)
			 bRes = true;		
	 }
	 return bRes;
 }

CGroupProduct* CSGroupTbl::getGroup(CString sCode)
{
	CGroupProduct* pGroup = NULL;
	MYSQL_RES * pResult = NULL;
	CString sNewQuery;
	mysql_query(conn,"SET character_set_results=utf8");
	sNewQuery.Format(_T("SELECT * FROM %s.%s where %s ='%s'"),g_sDatabase,TABLE_GROUP_PRODUCT,FIELD_CODE_GROUP,sCode);
	CT2A ascii(sNewQuery, CP_UTF8);		
	mysql_query(conn,ascii.m_psz);
	pResult = mysql_use_result(conn);
	if (pResult)
	{
		MYSQL_ROW row = mysql_fetch_row(pResult);
		if (row)
		{
			pGroup = new CGroupProduct;
			pGroup->setCode(CString(row[0]));
			pGroup->setName(CString (CA2W(row[1], CP_UTF8)));			
			pGroup->setType(atoi(row[2]));
			pGroup->setNote(CString (CA2W(row[3], CP_UTF8)));
		}
		mysql_free_result(pResult);
	}
	return pGroup;
}

 vector<CGroupProduct*> CSGroupTbl::getListGroup()
 {
	 vector<CGroupProduct*> vtGroup;

	 MYSQL_RES * pResult = NULL;
	 CString sNewQuery;
	 mysql_query(conn,"SET character_set_results=utf8");
	sNewQuery.Format(_T("SELECT * FROM %s.%s "),g_sDatabase,TABLE_GROUP_PRODUCT);
	 CT2A ascii(sNewQuery, CP_UTF8);		
	 mysql_query(conn,ascii.m_psz);
	 pResult = mysql_use_result(conn);
	 if (pResult)
	 {
		 MYSQL_ROW row = NULL;
		 while((row = mysql_fetch_row(pResult)) )
		 {
			 CGroupProduct* pGroup = new CGroupProduct;
			 pGroup->setCode(CString(row[0]));
			 pGroup->setName(CString (CA2W(row[1], CP_UTF8)));			
			 pGroup->setType(atoi(row[2]));
			 pGroup->setNote(CString (CA2W(row[3], CP_UTF8)));
			 vtGroup.push_back(pGroup);
		 }
		 mysql_free_result(pResult);
	 }
	 return vtGroup;
 }

 vector<CGroupProduct*> CSGroupTbl::getListGroupByType(const int& nType)
 {
	 vector<CGroupProduct*> vtGroup;

	 MYSQL_RES * pResult = NULL;
	 CString sNewQuery;
	 mysql_query(conn,"SET character_set_results=utf8");
	 sNewQuery.Format(_T("SELECT * FROM %s.%s WHERE %s = %d "),g_sDatabase,TABLE_GROUP_PRODUCT,FIELD_TYPE_GROUP,nType);
	 CT2A ascii(sNewQuery, CP_UTF8);		
	 mysql_query(conn,ascii.m_psz);
	 pResult = mysql_use_result(conn);
	 if (pResult)
	 {
		 MYSQL_ROW row = NULL;
		 while((row = mysql_fetch_row(pResult)) )
		 {
			 CGroupProduct* pGroup = new CGroupProduct;
			 pGroup->setCode(CString(row[0]));
			 pGroup->setName(CString (CA2W(row[1], CP_UTF8)));			
			 pGroup->setType(atoi(row[2]));
			 pGroup->setNote(CString (CA2W(row[3], CP_UTF8)));
			 vtGroup.push_back(pGroup);
		 }
		 mysql_free_result(pResult);
	 }
	 return vtGroup;
 }
 int  CSGroupTbl::getMaxCode()
 {
	 int nMax = 0;
	 MYSQL_RES * pResult = NULL;

	 CString sNewQuery;
	 mysql_query(conn,"SET character_set_results=utf8");
	 sNewQuery.Format(_T("SELECT %s ,MAX(%s)FROM %s.%s "),FIELD_CODE_GROUP,FIELD_CODE_GROUP,g_sDatabase,TABLE_GROUP_PRODUCT);
	 CT2A ascii(sNewQuery, CP_UTF8);		
	 mysql_query(conn,ascii.m_psz);
	 pResult = mysql_use_result(conn);
	 if (pResult)
	 {
		 MYSQL_ROW row = mysql_fetch_row(pResult);
		 if (row)
		 {
			 CString sTemplate;
			 sTemplate = row[1];
			 sTemplate.Replace(_T("NH"),_T("0"));
			 nMax = _tstoi(sTemplate);
		 }
		mysql_free_result(pResult);
	 }
	 return nMax;
 }
